package org.itboys.mobile.controller.mobile;

import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.service.mongo.mobile.SundayMobileCollectionService;
import org.itboys.mobile.service.mysql.mobile.product.SundayMobileProductService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：天街_金融_全球购_收藏_controller_mobile
 */
@Controller
@RequestMapping("/tianjie/mobile/finance/globe/collection")
public class TianJieMobileCollectionController extends BaseController{
    @Autowired
    private SundayMobileCollectionService collectionService;
    @Autowired
    private SundayMobileProductService productService;
    @RequestMapping(value="/getCollection",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【全球购-收藏】获取收藏数据")
    public void getCollection(HttpServletRequest request,
                           HttpServletResponse response,
                              @ApiParam(required=true, name="type", value="收藏类型 1商品")@RequestParam(value = "type", required = true)Integer type,
                              @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
                           @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
                           @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空。销量：saleNumber,价格：price") @RequestParam(value = "sort", required = false) String sort,
                           @ApiParam(required = false, name = "order", value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
                           @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                           @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            List results = new ArrayList();
            if(type== SundayCommonConstants.collection_type_product){
                results=productService.getCollectionProduct(request,memberId);
            }
           // List<SundayAddressDto> accounts=addressService.getAddress(memberId);
            ResponseMessage.success(results);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/globe/collection/getCollection----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    @RequestMapping(value="/collection",method= {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value="【全球购-收藏】添加或取消收藏。")
    public void collection(HttpServletRequest request,
                          HttpServletResponse response,
                     @ApiParam(required=true, name="type", value="收藏类型 1商品")@RequestParam(value = "type", required = true)Integer type,
                     @ApiParam(required=true, name="otherId", value="被收藏（商品）的id")@RequestParam(value = "otherId", required = true)Long otherId,
                     @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
                     @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign
    ) {
        try{
            //校验签名
            Long memberId = AccountIdHoldUtils.getMemberId();
            collectionService.collection(memberId,type,otherId);
            ResponseMessage.success();
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/tianjie/mobile/finance/globe/collection/collection----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
}
